package io.renren.modules.sys.service.impl;

import io.renren.common.annotation.DataFilter;
import io.renren.common.utils.Constant;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.SysDept1Dao;
import io.renren.modules.sys.entity.SysDept1Entity;
import io.renren.modules.sys.service.SysDept1Service;


@Service("sysDept1Service")
public class SysDept1ServiceImpl extends ServiceImpl<SysDept1Dao, SysDept1Entity> implements SysDept1Service {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {


        String deptname = (String)params.get("deptname");
        System.out.println("params:" + params + "**********");

        IPage<SysDept1Entity> page = this.page(
                new Query<SysDept1Entity>().getPage(params),
                new QueryWrapper<SysDept1Entity>()
                        .like(StringUtils.isNotBlank(deptname), "name", deptname).like("deptcode",params.get("deptcode"))
                        .apply(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
        );



        return new PageUtils(page);
    }
    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysDept1Entity> queryList(Map<String, Object> params){
        System.out.println("***********"+params+"**************");
        System.out.println("deptcode is " +params.get("deptcode")+ "**************");
        return baseMapper.queryList(params);
    }

    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysDept1Entity> queryList1(Map<String, Object> params){
        System.out.println("***********"+params+"**************");
        System.out.println("deptcode is " +params.get("deptcode")+ "**************");
        return baseMapper.queryList(params);
    }

    @Override
    public List<Long> queryDetpIdList(Long parentId) {

        return baseMapper.queryDetpIdList(parentId);
    }

    @Override
    public List<Long> getSubDeptIdList(Long deptId){
        //���ż��Ӳ���ID�б�
        List<Long> deptIdList = new ArrayList<>();

        //��ȡ�Ӳ���ID
        List<Long> subIdList = queryDetpIdList(deptId);
        getDeptTreeList(subIdList, deptIdList);

        return deptIdList;
    }

    /**
     * �ݹ�
     */
    private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
        for(Long deptId : subIdList){
            List<Long> list = queryDetpIdList(deptId);
            if(list.size() > 0){
                getDeptTreeList(list, deptIdList);
            }

            deptIdList.add(deptId);
        }
    }


    public Integer queryCount(Map<String, Object> params){

        return baseMapper.queryCount(params);
    }

    public List<SysDept1Entity> queryListbyParentId(Map<String, Object> params) {
        System.out.println("***********" + params + "**************");
        System.out.println("parentid is " + params.get("parentid") + "  **************");
        return baseMapper.queryListbyParentId(params);
    }
}
